<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="cheng5.du">
    
    <title>
        
            Docker基础知识 |
        
        cheng5.du
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/avatar.jpeg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"example.com","root":"/","language":"zh-CN"};
    KEEP.theme_config = {"toc":{"enable":false,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#0066CC","avatar":"/images/avatar.jpeg","favicon":"/images/avatar.jpeg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"人生海海，来日方长。"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":false,"preload":false},"code_copy":{"enable":false,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":false},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 6.1.0"></head>


<body>
<div class="progress-bar-container">
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                cheng5.du
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                分类
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/tags"
                            >
                                标签
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/netease"
                            >
                                网抑云
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于我
                            </a>
                        </li>
                    
                    
                </ul>
            </div>
            <div class="mobile">
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">分类</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/tags">标签</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/netease">网抑云</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于我</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">Docker基础知识</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.jpeg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">cheng5.du</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2020-10-12 21:35:16</span>
        <span class="mobile">2020-10-12 21:35</span>
    </span>
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/%E5%90%8E%E7%AB%AF%E8%BF%90%E7%BB%B4/">后端运维</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/Docker/">Docker</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h1 id="Docker入门知识"><a href="#Docker入门知识" class="headerlink" title="Docker入门知识"></a>Docker入门知识</h1><p>Docker 是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。一个完整的Docker有以下几个部分组成：DockerClient客户端；Docker Daemon守护进程；Docker Image镜像；DockerContainer容器。</p>
<h2 id="1-Docker-vs-虚拟机"><a href="#1-Docker-vs-虚拟机" class="headerlink" title="1.Docker vs 虚拟机"></a>1.Docker vs 虚拟机</h2><blockquote>
<p>服务器虚拟化解决的核心问题是资源调配，而容器解决的核心问题是应用开发、测试和部署。</p>
</blockquote>
<ol>
<li>首先需要明确一点，<strong>Docker容器不是虚拟机</strong>。</li>
<li>虚拟机与Docker框架，直观上来讲虚拟机多了一层Guest OS，同时<strong>Hypervisor会对硬件资源进行虚拟化，Docker直接使用硬件资源</strong>，所以资源利用率相对Docker低也是比较容易理解的。</li>
<li>openstack能够以10台&#x2F;min的速度创建虚拟机，在Docker面前就弱爆了，因为Docker是利用宿主机的系统内核，所以可以做到在几秒钟之内创建大量容器，它们的启动速度是在数量级上的差距。</li>
</ol>
<p><img src="https://user-images.githubusercontent.com/13176622/98439574-b773da00-212d-11eb-8156-9864427c838a.png"></p>
<h2 id="2-Docker主要特性"><a href="#2-Docker主要特性" class="headerlink" title="2.Docker主要特性"></a>2.Docker主要特性</h2><ol>
<li>文件系统隔离：每个进程容器运行在完全独立的根文件系统里。</li>
<li>资源隔离：可以使用cgroup为每个进程容器分配不同的系统资源，例如CPU和内存。</li>
<li>网络隔离：每个进程容器运行在自己的网络命名空间里，拥有自己的虚拟接口和IP地址。</li>
<li>写时复制：采用写时复制方式创建根文件系统，这让部署变得极其快捷，并且节省内存和硬盘空间。</li>
<li>日志记录：Docker将会收集和记录每个进程容器的标准流（stdout&#x2F;stderr&#x2F;stdin），用于实时检索或批量检索。</li>
<li>变更管理：容器文件系统的变更可以提交到新的映像中，并可重复使用以创建更多的容器。无需使用模板或手动配置。</li>
<li>交互式Shell：Docker可以分配一个虚拟终端并关联到任何容器的标准输入上，例如运行一个一次性交互shell。</li>
</ol>
<h2 id="3-Docker安装（Centos）"><a href="#3-Docker安装（Centos）" class="headerlink" title="3.Docker安装（Centos）"></a>3.Docker安装（Centos）</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 1.删除牢记版本（可以跳过）</span><br><span class="line">$ sudo yum remove docker</span><br><span class="line"></span><br><span class="line">// 2.安装必须的依赖</span><br><span class="line">$ sudo yum install -y yum-utils</span><br><span class="line"></span><br><span class="line">// 3.添加stable的Docker-ce源</span><br><span class="line">sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</span><br><span class="line"></span><br><span class="line">// 4.安装Docker-ce</span><br><span class="line">$ sudo yum install docker-ce docker-ce-cli dontainerd.io</span><br><span class="line"></span><br><span class="line">// 5.操作Docker</span><br><span class="line">$ systemctl start/stop/restart docker =&gt; 启动/停止/重启</span><br><span class="line">$ systemctl status docker =&gt; Docker状态</span><br></pre></td></tr></table></figure>

<h2 id="3-Docker常用命令"><a href="#3-Docker常用命令" class="headerlink" title="3.Docker常用命令"></a>3.Docker常用命令</h2><p><img src="https://user-images.githubusercontent.com/13176622/98439585-d07c8b00-212d-11eb-8661-902bf56f208b.png"></p>
<ul>
<li>Docker环境信息 — docker [info|version]</li>
<li>容器生命周期管理 — docker [create|exec|run|start|stop|restart|kill|rm|pause|unpause]</li>
<li>容器操作运维 — docker [ps|inspect|top|attach|wait|export|port|rename|stat]</li>
<li>容器rootfs命令 — docker [commit|cp|diff]</li>
<li>镜像仓库 — docker [login|pull|push|search]</li>
<li>本地镜像管理 — docker [build|images|rmi|tag|save|import|load]</li>
<li>容器资源管理 — docker [volume|network]</li>
<li>系统日志信息 — docker [events|history|logs]</li>
</ul>
<h2 id="4-Docker运行nginx示例"><a href="#4-Docker运行nginx示例" class="headerlink" title="4.Docker运行nginx示例"></a>4.Docker运行nginx示例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">// 1.拉取nginx镜像</span><br><span class="line">$ docker pull nginx</span><br><span class="line"></span><br><span class="line">// 2.启动nginx</span><br><span class="line">$ docker run --name my-nginx -p 80:80 -d nginx</span><br><span class="line"></span><br><span class="line">// 3.配置nginx</span><br><span class="line">$ docker <span class="built_in">exec</span> -it my-nginx bash</span><br><span class="line"></span><br><span class="line">// 4.进入到nginx目录下</span><br><span class="line">$ <span class="built_in">cd</span> /etc/nginx</span><br><span class="line"></span><br><span class="line">// 5.安装vim</span><br><span class="line">$ apt-get update</span><br><span class="line">$ apt-get install vim</span><br><span class="line"></span><br><span class="line">// 6.编辑nginx.conf</span><br><span class="line">$ vim nginx.conf</span><br><span class="line"></span><br><span class="line">// 7.暂停容器</span><br><span class="line">$ docker stop nginx</span><br><span class="line"></span><br><span class="line">// 8.重启</span><br><span class="line">$ docker start nginx</span><br></pre></td></tr></table></figure>
<h2 id="5-注意事项"><a href="#5-注意事项" class="headerlink" title="5.注意事项"></a>5.注意事项</h2><h3 id="5-1修改nginx配置文件时遇到了403的问题"><a href="#5-1修改nginx配置文件时遇到了403的问题" class="headerlink" title="5.1修改nginx配置文件时遇到了403的问题"></a>5.1修改nginx配置文件时遇到了403的问题</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 1.nginx.conf中user nobody</span><br><span class="line">修改例如：</span><br><span class="line">user root;</span><br><span class="line"></span><br><span class="line">// 2.根目录 location / &#123;&#125;时，</span><br><span class="line">// root /usr/share/nginx/html可以用，</span><br><span class="line">// 但是修改为 <span class="built_in">alias</span> /var/www/test报403</span><br><span class="line">// 注意最后的斜杠（/）</span><br><span class="line"><span class="built_in">alias</span> /var/www/test/</span><br><span class="line"></span><br><span class="line">// 3.二级域名访问时 lacation /test &#123;&#125;时，</span><br><span class="line">// 不加斜杠是ok的</span><br><span class="line"></span><br><span class="line"><span class="built_in">alias</span> /var/www/test</span><br><span class="line">try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ /test/index.html</span><br></pre></td></tr></table></figure>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/tags/Docker/">#Docker</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2020/10/28/Linux%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Linux基础知识</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2018/05/21/%E5%86%99%E7%BB%9930%E5%B2%81%E7%9A%84%E8%87%AA%E5%B7%B1/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">写给30岁的自己</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">cheng5.du</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    

    <div class="image-viewer-container">
    <img src="">
</div>


    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>








<div class="post-scripts">
    
</div>



</body>
</html>
